<?php
/**
 * Created by PhpStorm.
 * User: 快定
 * Date: 2021/7/9
 * Time: 21:41
 */

App::uses('CakeSession', 'Model/Datasource');

class RoleController extends AppController
{
    public $uses = array('Admin','Auth');
    public $components = array('publicFunction');
    public $layout = null;
    /*
     * 展示全部角色的信息
     */
    public function index()
    {
        $request = $_SERVER['REQUEST_METHOD'] ? $_POST : $_GET;
        $data = $this->publicFunction->filterParams($request);
        if ($data['role_name']){
            $where['role_name Like'] = "%{$data['role_name']}%";
        }
        //获取全部信息
        $res = $this->Role->getAll($where);

        $this->set(array('params' => $res,'role_name'=>$data['role_name']));
    }


    //角色授权页面
    public function grant_auth()
    {
        $request = $_SERVER['REQUEST_METHOD'] === "POST" ? $_POST : $_GET;
        //获取当前用户信息
        $data = $this->publicFunction->filterParams($request);
        $id = $data['id'];
        $allAuth = $this->Auth->getAuth();
        $myAuth = $this->Role->find_auth($id);
        $myAuthArr = explode(',',$myAuth);
        $allAuthArr = [];
        foreach ($allAuth as $k => $v) {
            $allAuthArr[$k] = $v['Auth'];
            $allAuthArr[$k]['status'] = 0;
            if (in_array($v['Auth']['id'],$myAuthArr)){
                $allAuthArr[$k]['status'] = 1;
            }
        }
       $list = $this->publicFunction->get_tree_list($allAuthArr);
       $this->set(['list'=>$list,'id'=>$id]);
    }

    /*
     * 新增、修改角色信息
     */
    public function dellRole()
    {
        $post = $_POST;
        //修改操作
        if ($post['id']) {
            if (in_array('', $post)) {
                $res = array('code' => 400, 'msg' => 'update error');
                exit(json_encode($res));
            } else {
                $data = $this->Role->dellRole($post);
                if ($data) {
                    $res = array('code' => 200, 'msg' => 'update success');
                    exit(json_encode($res));
                } else {
                    $res = array('code' => 400, 'msg' => 'update error');
                    exit(json_encode($res));
                }
            }
        } else {
            //新增操作
            if ($post['role_name'] == '') {
                $res = array('code' => 400, 'msg' => 'insert error');
                exit(json_encode($res));
            } else {
                $data = $this->Role->dellRole($post);
                if ($data) {
                    $res = array('code' => 200, 'msg' => 'insert success');
                    exit(json_encode($res));
                } else {
                    $res = array('code' => 400, 'msg' => 'insert error');
                    exit(json_encode($res));
                }
            }
        }
    }

    /*
     * 删除用户信息
     */
    public function delRole()
    {
        $id = $_POST['role_id'];
        $data = $this->Role->delRole($id);
        if ($data) {
            $res = array('code' => 200, 'msg' => 'success');
        } else {
            $res = array('code' => 400, 'msg' => 'error');
        }
        exit(json_encode($res));
    }

    public function authSave()
    {
        $request = $_SERVER['REQUEST_METHOD'] ? $_POST : $_GET;
        $data = $this->publicFunction->filterParams($request);
        $data['auth_ids'] = implode(',', $data['auth']);
        unset($data['auth']);
        $res = $this->Role->dellRole($data);
        if ($res) {
            $this->redirect('/role/index');
        }
    }
}

